﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CleanPRCache
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            FindDir();
        }

        void FindDir()
        {
            textBox1.Text = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Adobe\\Common\\Media Cache Files";
        }

        private void button_open_Click(object sender, EventArgs e)
        {
            folder.RootFolder = Environment.SpecialFolder.ApplicationData;
            if (folder.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = folder.SelectedPath;
            }
        }

        private void button_clean_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                DeleteFolder(textBox1.Text);
                MessageBox.Show("删除完成！");
            }
        }

        void DeleteFolder(string dir)
        {
            foreach (string d in Directory.GetFileSystemEntries(dir))
            {
                if (File.Exists(d))
                {
                    FileInfo fi = new FileInfo(d);
                    if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
                        fi.Attributes = FileAttributes.Normal;
                    File.Delete(d);//直接删除其中的文件
                }
                else
                {
                    DirectoryInfo d1 = new DirectoryInfo(d);
                    if (d1.GetFiles().Length != 0)
                    {
                        DeleteFolder(d1.FullName);////递归删除子文件夹
                    }
                    Directory.Delete(d);
                }
            }
        }
    }
}
